package com.kll.tv.views.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.kll.carolinehmibiz.net.bean.GetDiseaseResponse
import com.kll.tv.R
import com.kll.tv.databinding.ItemDiseasesBinding

class DiseasesAdapter(var context: Context) : RecyclerView.Adapter<DiseasesAdapter.InnerHolder>() {
    private var mItemClickListener: OnDiseasesListItemClickListener? = null

    // 数据
    var data: ArrayList<GetDiseaseResponse.DiseaseBean> = ArrayList()
        set(value) {
            if (value == null) {
                data.clear()
            } else {
                field = value
            }
            notifyDataSetChanged()
        }

    fun addData(data: ArrayList<GetDiseaseResponse.DiseaseBean>) {
        if (data != null) {
            this.data.addAll(data)
            notifyDataSetChanged()
        }
    }

    inner class InnerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var binding = ItemDiseasesBinding.bind(itemView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): InnerHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_diseases, parent, false)
        return InnerHolder(view)
    }

    override fun onBindViewHolder(holder: InnerHolder, position: Int) {
        val data = data[position]

        holder.itemView.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                holder.binding.tvEnglishName.setTextColor(Color.parseColor("#FFFFFF"))
                holder.binding.tvChinaName.setTextColor(Color.parseColor("#FFFFFF"))
            } else {
                holder.binding.tvEnglishName.setTextColor(Color.parseColor("#00374D"))
                holder.binding.tvChinaName.setTextColor(Color.parseColor("#00374D"))
            }
        }

        holder.binding.tvChinaName.text = data.zh_name
        holder.binding.tvEnglishName.text = data.en_name

        if (mItemClickListener != null) {
            holder.itemView.setOnClickListener {
                mItemClickListener!!.onDiseasesItemClick(data)
            }
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    interface OnDiseasesListItemClickListener {
        fun onDiseasesItemClick(data: GetDiseaseResponse.DiseaseBean)
    }

    fun setOnDiseasesListItemClickListener(listener: OnDiseasesListItemClickListener) {
        this.mItemClickListener = listener
    }

}